package controller

import (
	"context"
	"github.com/gin-gonic/gin"
	"github.com/yyangl/yoyo/application"
	"github.com/yyangl/yoyo/proto"
	"google.golang.org/grpc"
)

type Menu struct {
	application.BaseController
}

func (c *Menu) RegRouter() {
	c.Get("/menus", c.Menus)
}

func (c *Menu) Menus(ctx *gin.Context) {
	_, sAddr, err := c.GetApp().GetService("admin")
	if err != nil {
		c.ServiceErr(ctx)
		return
	}
	conn, err := grpc.Dial(sAddr, grpc.WithInsecure())
	if err != nil {
		c.Err(ctx, err.Error())
		return
	}
	defer conn.Close()
	cli := proto.NewAdminServiceClient(conn)
	req := &proto.MenusReq{
		Uid: 1,
	}
	cx := context.Background()
	reps, err := cli.Menus(context.WithValue(cx, "a", "b"), req)
	if err != nil {
		c.Err(ctx, err.Error())
		return
	}

	c.OK(ctx, reps)
}
